const { Quest, Scorelimit, Option, Optiontype, Childoption } = require('../models')

//评分问卷
let ctwj_g = async ctx => {
    ctx.render('createwj.html')
}

//发布问卷(评分问卷)
let addQuest_p = async ctx => {
    let { title = '', maxScore = '', endTime, remark, optionArr = [] } = ctx.request.body;

    if (!title.trim() || parseInt(maxScore).toString() === 'NaN' || optionArr.length === 0) return ctx.body = { code: 443, err: '创建失败，请输入必要内容' }


    let num = await Quest.count();
    num = 10000 + num;

    let quest = await Quest.create({
        code: num.toString(),
        questName: title,
        questTypeId: 1,
        questStateId: 1,
        deadline: endTime,
        remarks: remark
    })

    await Scorelimit.create({
        questId: quest.getDataValue('id'),
        score: maxScore
    })

    let arr = []

    optionArr.forEach(item => {
        let obj = {
            content: item.content,
            questId: quest.getDataValue('id'),
            optionTypeId: 4,
            remarks: item.remark
        }
        arr.push(obj)
    })

    await Option.bulkCreate(arr)

    ctx.body = {
        code: 200,
        msg: '创建成功'
    }




}


//保存问卷
let saveQuest_p = async ctx => {
    let { title, remark, optionArr } = ctx.request.body;


    if (!title.trim() || optionArr.length === 0) return ctx.body = { code: 443, err: '创建失败，请输入必要内容' }


    let num = await Quest.count();
    num = 10000 + num;

    let quest = await Quest.create({
        code: num.toString(),
        questName: title,
        questTypeId: 1,
        questStateId: 3,
        remarks: remark
    })

    let arr = []

    optionArr.forEach(item => {
        let obj = {
            content: item.content,
            questId: quest.getDataValue('id'),
            optionTypeId: 4,
            remarks: item.remark
        }
        arr.push(obj)
    })

    await Option.bulkCreate(arr)

    ctx.body = {
        code: 200,
        msg: '创建成功'
    }
}


//普通问卷
let ccomwj_g = async ctx => {

    let { questId = 0 } = ctx.query
    // console.log(questId);
    ctx.render('creawjcommon.html', { questId })
}

let addcomquest_p = async ctx => {
    let { title = '', remark = '', endTime = null, list = [] } = ctx.request.body;

    // console.log(ctx.request.body);

    if (!title.trim() || list.length === 0) return ctx.body = { err: 340, msg: '错误' }

    let num = await Quest.count();
    num = 10000 + num;

    // console.log(ctx.request.body);

    let quest = await Quest.create({
        code: num.toString(),
        questName: title,
        questTypeId: 2,
        questStateId: 1,
        deadline: endTime,
        remarks: remark
    })

    let Options = []

    list.forEach(item => {
        let typeId;
        if (item.type === 1) {
            typeId = 2
        } else if (item.type === 2) {
            typeId = 3
        } else if (item.type === 3) {
            typeId = 1
        }

        let obj = {
            content: item.content,
            questId: quest.getDataValue('id'),
            optionTypeId: typeId,
            remarks: item.remark
        }
        Options.push(obj)
    })

    let parOp = await Option.bulkCreate(Options)


    let childOptions = []
    parOp.forEach((item, index) => {
        item.childlist = list[index].options
    })

    parOp.forEach(item => {

        item.childlist.forEach(child => {
            childOptions.push({
                optionId: item.id,
                content: child.content
            })
        })
    })


    await Childoption.bulkCreate(childOptions);


    ctx.body = { code: 200, msg: 'ok' }
}

let savecommquest_p = async ctx => {
    let { title = '', remark = '', list = [] } = ctx.request.body;

    if (!title.trim() || list.length === 0) return ctx.body = { err: 340, msg: '错误' }

    let num = await Quest.count();
    num = 10000 + num;

    // console.log(ctx.request.body);

    let quest = await Quest.create({
        code: num.toString(),
        questName: title,
        questTypeId: 2,
        questStateId: 3,
        remarks: remark
    })

    let Options = []

    list.forEach(item => {
        let typeId;
        if (item.type === 1) {
            typeId = 2
        } else if (item.type === 2) {
            typeId = 3
        } else if (item.type === 3) {
            typeId = 1
        }

        let obj = {
            content: item.content,
            questId: quest.getDataValue('id'),
            optionTypeId: typeId,
            remarks: item.remark
        }
        Options.push(obj)
    })

    let parOp = await Option.bulkCreate(Options)


    let childOptions = []
    parOp.forEach((item, index) => {
        item.childlist = list[index].options
    })

    parOp.forEach(item => {

        item.childlist.forEach(child => {
            childOptions.push({
                optionId: item.id,
                content: child.content
            })
        })
    })


    await Childoption.bulkCreate(childOptions);


    ctx.body = { code: 200, msg: 'ok' }
}


module.exports = {
    '/home/createwj': ['get', ctwj_g],
    '/home/addquest/wjs': ['post', addQuest_p],
    '/home/savequest/wjs': ['post', saveQuest_p],
    '/home/ctcommonwj': ['get', ccomwj_g],
    '/home/wjs/addcommonquest': ['post', addcomquest_p],
    '/home/wjs/savecommonquest': ['post', savecommquest_p]
}